package com.simple.service.fee;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.simple.po.FeeDetail;
import com.xunj.core.CoreDao;
import com.xunj.core.ParaUnit;
import com.xunj.po.SysCodeDict;
import com.xunj.service.system.CodeService;
import com.xunj.service.system.IReportEngine;
import com.xunj.util.Common;
import com.xunj.util.Util;

public class FeeService implements IReportEngine{
	
	private CoreDao dao;

	public FeeService(CoreDao dao) {
		this.dao = dao;
	}

	
	/**
	 * 返回费用上报编码
	 * @param filePath
	 * @throws Exception
	 */
	public String getFeeReportCode() throws Exception
	{
		String date = Common.getCurrentdate("yyyyMM");  

		String hql= "select new Map(max(fe.feeReportCode) as feeReportCode) from FeeReport fe where fe.feeReportCode like '"+date+"%' ";		
		
		List billList = dao.findAll(hql);
		String newCode="";
		if (!billList.isEmpty())
		{
			Map map = (Map) billList.get(0);
			String maxCode = (String) map.get("feeReportCode");
			if(maxCode!=null)
				newCode = Common.createSequence(map.get("feeReportCode").toString().replace(date, ""), "0", 4);
			else
				newCode = "0001";
		}
		newCode=date+newCode;
		return newCode;
	}
	/**
	 * 费用明细与费用核销信息组合查询
	 * @param empEmployee
	 * @param empEmployeeExtend
	 * @param fieldString
	 * @param orderField
	 * @return
	 * @throws Exception
	 */
	public List queryFeeJoin(String appendFieldHql,String appendCondition,String orderHql,boolean isSpPage) throws Exception
	{
		String hql = "";
		String fieldHql = "";
		
		fieldHql ="feeDetail.feeId as feeId," +
		"feeDetail.feeTarget as feeTarget," +
		"feeDetail.targetId as targetId," +
		"feeDetail.targetName as targetName," +
		"feeDetail.areaId as areaId," +
		"feeDetail.areaName as areaName," +
		"feeDetail.areaDeptId as areaDeptId," +
		"feeDetail.areaDeptName as areaDeptName," +
		"feeDetail.feeDate as feeDate," +
		"feeDetail.feeTypeId as feeTypeId," +
		"feeDetail.feeTypeName as feeTypeName," +
		"feeDetail.financeSupervisorId as financeSupervisorId," +
		"feeDetail.financeSupervisorName as financeSupervisorName," +
		"feeDetail.billAmount as billAmount," +
		"feeDetail.billCount as billCount," +
		"feeDetail.certificateAmount as certificateAmount," +
		"feeDetail.certificateCount as certificateCount," +
		"feeDetail.createDate as createDate," +
		"feeDetail.feeDetailRemark as feeDetailRemark," +
		"feeDetail.feeReportId as feeReportId," +
		"feeDetail.state as state," +
		"feeDetail.updateState as updateState," +
		"feeDetail.isAddState as isAddState," +
		"feeDetail.createUserId as createUserId," +
		"feeDetail.createUserName as createUserName," +
		"feeDetail.note1 as note1," +
		"feeDetail.note2 as note2," +
		"feeDetail.note3 as note3," +
		"feeDetail.note4 as note4," +
		"feeDetail.note5 as note5,";
		fieldHql += "feeWrittenOff.billAmountAdjust as billAmountAdjust," +
		"feeWrittenOff.billCountAdjust as billCountAdjust," +
		"feeWrittenOff.certificateAmountAdjust as certificateAmountAdjust," +
		"feeWrittenOff.adjustRemark as adjustRemark," +
		"feeWrittenOff.certificateCountAdjust as certificateCountAdjust," +
		"feeWrittenOff.billAmountWrittenOff as billAmountWrittenOff," +
		"feeWrittenOff.billCountWrittenOff as billCountWrittenOff," +
		"feeWrittenOff.certificateAmountWrittenOff as certificateAmountWrittenOff," +
		"feeWrittenOff.certificateCountWrittenOff as certificateCountWrittenOff," +
		"feeWrittenOff.writtenOffRemark as writtenOffRemark," +
		"feeWrittenOff.billAmountLeaveReview as billAmountLeaveReview," +
		"feeWrittenOff.billCountLeaveReview as billCountLeaveReview," +
		"feeWrittenOff.certificateAmountLeaveReview as certificateAmountLeaveReview," +
		"feeWrittenOff.certificateCountLeaveReview as certificateCountLeaveReview," +
		"feeWrittenOff.leaveReviewRemark as leaveReviewRemark," +
		"feeWrittenOff.billAmountLeaveReviewEarly as billAmountLeaveReviewEarly," +
		"feeWrittenOff.billCountLeaveReviewEarly as billCountLeaveReviewEarly," +
		"feeWrittenOff.certificateAmountLeaveReviewEarly as certificateAmountLeaveReviewEarly," +
		"feeWrittenOff.certificateCountLeaveReviewEarly as certificateCountLeaveReviewEarly," +
		"feeWrittenOff.leaveReviewEarlyRemark as leaveReviewEarlyRemark," +
		"feeWrittenOff.billAmountRetruned as billAmountRetruned," +
		"feeWrittenOff.billCountRetruned as billCountRetruned," +
		"feeWrittenOff.certificateAmountRetruned as certificateAmountRetruned," +
		"feeWrittenOff.certificateCountRetruned as certificateCountRetruned," +
		"feeWrittenOff.leaveReviewRetrunedRemark as leaveReviewRetrunedRemark";
		
		hql="select new Map("+fieldHql+appendFieldHql+") from " +
				"FeeDetail feeDetail," +
				"FeeWrittenOff feeWrittenOff where feeWrittenOff.feeId=feeDetail.feeId "+appendCondition;
		if(orderHql!=null&&!"".equals(orderHql))
			hql+=" "+orderHql;
		int rowCount=0;
		if(isSpPage)
		{
			String countHql = "select count(*) from " +
			"FeeDetail feeDetail," +
			"FeeWrittenOff feeWrittenOff where feeWrittenOff.feeId=feeDetail.feeId "+appendCondition;
			rowCount=dao.findRowCount(countHql);
			return dao.findByPage(hql, rowCount, new ArrayList());
		}
		else
			return dao.findAll(hql);
	}
	/**
	 * 整理费用类别子集数据，将子集以list方式放入父级数据中
	 * @param currentCodeList
	 * @param pid
	 * @return
	 */
	public List getFeeTypeList(List currentCodeList,String pid)
	{
		List feeTypeList = new ArrayList();
		for(int i=0;i<currentCodeList.size();i++)
		{
			SysCodeDict cd = (SysCodeDict) currentCodeList.get(i);
			if(cd.getParentCodeId().equals(pid))
			{
				Map codeMap = new HashMap();
				boolean hasChild = false;
				codeMap.put("codeId", cd.getCodeId());
				codeMap.put("codeName", cd.getCodeName());
				codeMap.put("parentCodeId", cd.getParentCodeId());
				codeMap.put("sign", "o");//原字典码数据
				
				for(int j=0;j<currentCodeList.size();j++)
				{
					SysCodeDict cd1 = (SysCodeDict) currentCodeList.get(j);
					//该级别的子集
					if(cd1.getParentCodeId().equals(pid))
						hasChild = true;
				}
				if(hasChild)
					codeMap.put("childList", getFeeTypeList(currentCodeList,cd.getCodeId()));
				else
					codeMap.put("childList", new ArrayList());
				feeTypeList.add(codeMap);
			}
			if(i==currentCodeList.size()-1)
			{
				if(pid.length()==3)
				{
					//一级
					Map codeMap = new HashMap();
					codeMap.put("codeId", pid);
					codeMap.put("codeName", "合计");
					codeMap.put("parentCodeId", cd.getParentCodeId());
					codeMap.put("sign", "z");//汇总统计列
	
					List hjL2List =  new ArrayList();
					codeMap.put("childList",hjL2List);
					
					Map codeMapl2 = new HashMap();
					codeMapl2.put("codeId", pid);
					codeMapl2.put("codeName", "");
					codeMapl2.put("parentCodeId", cd.getParentCodeId());
					codeMapl2.put("sign", "z");//汇总统计列
					
					List hjL3List =  new ArrayList();
					codeMapl2.put("childList",hjL3List);
					hjL2List.add(codeMapl2);
					
					
					Map codeMapl3 = new HashMap();
					codeMapl3.put("codeId", pid);
					codeMapl3.put("codeName", "");
					codeMapl3.put("parentCodeId", cd.getParentCodeId());
					codeMapl3.put("sign", "z");//汇总统计列
					codeMapl3.put("childList",new ArrayList());
					hjL3List.add(codeMapl3);
					
					
					feeTypeList.add(codeMap);
				}	
				if(pid.length()==6)
				{
					Map codeMap = new HashMap();
					codeMap.put("codeId", pid);
					codeMap.put("codeName", "小计");
					codeMap.put("parentCodeId", cd.getParentCodeId());
					codeMap.put("sign", "t");//汇总统计列
	
					List xjL3List =  new ArrayList();
					
					Map codeMapl3 = new HashMap();
					codeMapl3.put("codeId", pid);
					codeMapl3.put("codeName", "小计");
					codeMapl3.put("parentCodeId", cd.getParentCodeId());
					codeMapl3.put("sign", "t");//汇总统计列
					xjL3List.add(codeMapl3);
					codeMap.put("childList",xjL3List);
					feeTypeList.add(codeMap);
				}	
			}
		}
		return feeTypeList;
	}
	
	/**
	 * 整理费用类别数据，补充二三级中不足的级别内容
	 * @param feeTypeList
	 */
	public void disposalFeeTypeList(List feeTypeList)
	{
		for(int i=0;i<feeTypeList.size();i++)
		{
			Map codeMap = (Map) feeTypeList.get(i);
			String codeId = (String) codeMap.get("codeId");
			//三级以下整理数据
			if(codeId.length()<=9)
			{
				List childList = (List)codeMap.get("childList");
				//如果没有子集把自己放入子集中
				if(childList!=null&&childList.size()==0)
				{
					Map codeMap1 = new HashMap();
					codeMap1.put("codeId", codeId);
					codeMap1.put("codeName", codeMap.get("codeName"));
					codeMap1.put("parentCodeId", codeMap.get("codeId"));
					codeMap1.put("sign", "n");
					childList.add(codeMap1);
				}
				if(codeMap.get("sign").equals("o"))
					disposalFeeTypeList(childList);
			}
		}
	}
	/**
	 * 费用明细信息导出
	 * @return
	 * @throws Exception
	 */
	public Map exportFeeDetail() throws Exception
	{
		HttpServletRequest request = ServletActionContext.getRequest();
		FeeDetail feeDetail = new FeeDetail();
		Map map=request.getParameterMap();
		Iterator its = map.entrySet().iterator();
		Util u = new Util();
		while (its.hasNext()) {
			Entry e = (Entry) its.next();
			String name = (String) e.getKey();
			if(name.indexOf("feeDetail.")!=-1)
			{
				String[] values = (String[]) e.getValue();
				if(values[0] !=null && !values[0].equals(""))
					u.setPropertyValue(feeDetail, name.substring(10, name.length()), values[0]);
			}
		}
		
		
		ArrayList<ParaUnit> paralist = new ArrayList<ParaUnit>();

		paralist.add(new ParaUnit("createDate", ParaUnit.DESC,ParaUnit.ORDER));
		paralist.add(new ParaUnit("state", feeDetail.getState(), ParaUnit.EQ));
		if(feeDetail.getAreaId()!=null&&!feeDetail.getAreaId().equals(""))
			paralist.add(new ParaUnit("areaId", feeDetail.getAreaId(), ParaUnit.EQ));
		if(feeDetail.getAreaDeptId()!=null&&!feeDetail.getAreaDeptId().equals(""))
			paralist.add(new ParaUnit("areaDeptId", feeDetail.getAreaDeptId(), ParaUnit.EQ));
		String feeDateFrom = request.getParameter("feeDateFrom");
		String feeDateTo = request.getParameter("feeDateTo");
		if(!feeDateFrom.equals(""))
		{
			paralist.add(new ParaUnit("feeDate",Common.newDate(feeDateFrom+" 00:00:00", "yyyy-MM-dd"), ParaUnit.GE));
			request.setAttribute("feeDateFrom", feeDateFrom);
		}
		if(!feeDateTo.equals(""))
		{
			paralist.add(new ParaUnit("feeDate",Common.newDate(feeDateTo+" 23:59:59", "yyyy-MM-dd"), ParaUnit.LE));
			request.setAttribute("feeDateTo", feeDateTo);
		}
		if(feeDetail.getFeeTypeId()!=null&&!feeDetail.getFeeTypeId().equals(""))
			paralist.add(new ParaUnit("feeTypeId", feeDetail.getFeeTypeId(), ParaUnit.EQ));
		if(feeDetail.getFeeTarget()!=null&&!feeDetail.getFeeTarget().equals(""))
			paralist.add(new ParaUnit("feeTarget", feeDetail.getFeeTarget(), ParaUnit.EQ));
		if(feeDetail.getTargetName()!=null&&!feeDetail.getTargetName().equals(""))
			paralist.add(new ParaUnit("targetName", feeDetail.getTargetName(), ParaUnit.LIKE));
			
		List list = dao.criteriaQuery(FeeDetail.class, paralist);
		
		List resultList = new ArrayList();
		ServletContext ctx = ServletActionContext.getServletContext();
		List codeList = (List) ctx.getAttribute("CodeDict");
		for(int i=0;i<list.size();i++)
		{
			FeeDetail fd = (FeeDetail) list.get(i);
			HashMap feeDetailMap = new HashMap();
			u.copyBeanToMap(fd, feeDetailMap);
			feeDetailMap.put("feeTypeL1",CodeService.getCodeNameByCodeId(codeList, fd.getFeeTypeId().substring(0, 6)));
			resultList.add(feeDetailMap);
		}
		
		Map beans = new HashMap();
		beans.put("datalist", resultList);
		return beans;
	}
}
